package cn.lili.modules.order.order.entity.dto;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.order.order.entity.dos.OrderFlow;
import cn.lili.modules.order.order.entity.enums.OrderFlowStatusEnum;
import cn.lili.modules.order.order.entity.enums.ProfitSharingStatusEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 流水查询DTO
 *
 * @author Chopper
 * @version v1.0
 * 2021-12-08 10:53
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderFlowQueryDTO extends PageVO implements Serializable {

    @Serial
    private static final long serialVersionUID = 7054208840223239847L;

    @ApiModelProperty(value = "订单编号")
    private String orderSn;
    @ApiModelProperty(value = "订单流水编号")
    private String orderFlowSn;

    @ApiModelProperty(value = "过滤只看分销订单")
    private Boolean justDistribution;

    @ApiModelProperty(value = "店铺ID")
    private String storeId;

    @ApiModelProperty(value = "供应商ID")
    private String supplierId;

    @ApiModelProperty(value = "会员ID")
    private String memberId;

    /**
     * @see OrderFlowStatusEnum
     */
    @ApiModelProperty(value = "审核状态")
    private String orderFlowStatus;

    @ApiModelProperty(value = "审核状态")
    private Boolean isProfitSharing;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "开始时间")
    private Date startDate;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "结束时间")
    private Date endDate;

    @ApiModelProperty(value = "是否是供应商查询: 这里有一个特殊场景，即供应商查询时，" + "它可以以店铺和供应商两个身份进行查询，" + "如果是店铺发生的采购行为则是以店铺身份查询，" + "如果是会员通过其他店铺购买当前店铺发布的代发商品以供应商身份查询")
    private Boolean supplierSearch;

    @ApiModelProperty(value = "支付方式")
    private String paymentMethod;

    @ApiModelProperty(value = "待审核端")
    private String waitAuditScene;

    @ApiModelProperty(value = "结算状态")
    private String settlementStatus;

    @ApiModelProperty(value = "城市分站ID")
    private String urbanId;

    @ApiModelProperty(value = "城市分站下供应商ID集合")
    private List<String> storeIds;

    @ApiModelProperty(value = "分销商名称")
    private String distributionName;
    @ApiModelProperty(value = "店主/分销商 用户id")
    private String distributionMemberId;
    @ApiModelProperty("类型 : 1,直推 2,间推")
    private Integer queryType;
    @ApiModelProperty("时间筛选")
    private String flowTime;
    @ApiModelProperty("结算状态: 1,已结算,2,未结算,3,已退单")
    private Integer profitSharingType;
    @ApiModelProperty("结算状态: 已结算,未结算,已退单")
    private String profitSharing;


    @ApiModelProperty("地址code条件")
    private String joinAddressCode;
    @ApiModelProperty("开始日期条件筛选")
    private LocalDate startLocalDate;
    @ApiModelProperty("结束日期条件筛选")
    private LocalDate endLocalDate;

    /**
     * 生成查询wrapper
     *
     * @return 查询wrapper
     */
    public LambdaQueryWrapper<OrderFlow> generatorQueryWrapper() {


        LambdaQueryWrapper<OrderFlow> lambdaQueryWrapper = Wrappers.lambdaQuery();
        //分销订单过滤是否判定
        lambdaQueryWrapper.gt(this.getJustDistribution() != null && this.getJustDistribution(), OrderFlow::getDistributionSettlementPrice, 0);

        //售后编号判定
        lambdaQueryWrapper.like(CharSequenceUtil.isNotEmpty(this.getOrderSn()), OrderFlow::getOrderSn, this.getOrderSn());
        //售后编号判定
        lambdaQueryWrapper.like(CharSequenceUtil.isNotEmpty(this.getOrderFlowSn()), OrderFlow::getSn, this.getOrderFlowSn());

        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(this.getStoreId()), OrderFlow::getStoreId, this.getStoreId());

        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(this.getSupplierId()), OrderFlow::getSupplierId, this.getSupplierId());

        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(this.getOrderFlowStatus()), OrderFlow::getOrderFlowStatus, this.getOrderFlowStatus());

        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(this.getDistributionName()), OrderFlow::getDistributionName, this.getDistributionName());

        //分账判定
        lambdaQueryWrapper.eq(isProfitSharing != null, OrderFlow::getProfitSharing, ProfitSharingStatusEnum.FINISHED.name());

        if (CollectionUtil.isNotEmpty(this.getStoreIds())) {
            lambdaQueryWrapper.in(OrderFlow::getSupplierId, storeIds);
        }
        //特殊供应商身份查询订单流水
        if (supplierSearch != null && supplierSearch) {
            lambdaQueryWrapper.and(wrapper -> wrapper.eq(OrderFlow::getStoreId, UserContext.getCurrentUser().getExtendId()).or().eq(OrderFlow::getSupplierId,
                    UserContext.getCurrentUser().getExtendId()));
        }

        //按时间查询
        lambdaQueryWrapper.ge(startDate != null, OrderFlow::getCreateTime, startDate);
        lambdaQueryWrapper.le(endDate != null, OrderFlow::getCreateTime, endDate);


        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(this.getPaymentMethod()), OrderFlow::getPaymentMethod, this.getPaymentMethod());


        if (CharSequenceUtil.isNotEmpty(this.getWaitAuditScene())) {
            switch (SceneEnums.valueOf(this.getWaitAuditScene())) {
                case STORE -> lambdaQueryWrapper.eq(
                        OrderFlow::getStoreVerify, false);
                case SUPPLIER -> lambdaQueryWrapper.eq(
                        OrderFlow::getSupplierVerify, false);
                case MANAGER -> lambdaQueryWrapper.eq(
                        OrderFlow::getPlatformVerify, false);
                case URBAN_SUBSTATION -> lambdaQueryWrapper.eq(
                        OrderFlow::getUrbanVerify, false);
                default -> {
                    throw new ServiceException("未知的审核端");
                }
            }
        }

        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(this.getSettlementStatus()), OrderFlow::getProfitSharing, this.getSettlementStatus());

        lambdaQueryWrapper.orderByDesc(OrderFlow::getCreateTime);

        return lambdaQueryWrapper;
    }

}
